package com.humanminoritygame.web.exception;

import java.io.PrintWriter;
import java.io.StringWriter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;


public class EmailNotificationService implements NotificationService {
  private MailSender mailSender;
  private SimpleMailMessage templateMessage;

  //public static final Log log = LogFactory.getLog(EmailNotificationService.class);

  @Autowired
  public EmailNotificationService(MailSender mailSender, SimpleMailMessage templateMessage){
    this.mailSender = mailSender;
    this.templateMessage = templateMessage;
  }

  public void sendNotification(String message, Exception exception) {

    SimpleMailMessage msg = new SimpleMailMessage(this.templateMessage);
    StringWriter stackTrace = new StringWriter();
    exception.printStackTrace(new PrintWriter(stackTrace));
    msg.setText("Application Message - " + message + " : " 
        + exception.getMessage()
        +"\n Strack Trace: \n"
        + stackTrace.toString());
    try{
      this.mailSender.send(msg);
    } catch(MailException ex) {
      // simply log it and go on...
     // log.fatal("Email Notification message could not sent", ex);
      //System.out.println("send notification EXCEPTION!!!");
      throw new RuntimeException(ex);
    }
  }
}
